package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.BalanceSnapshot;
import com.qian.api.ddj.service.IBalanceSnapshotService;
import com.qian.service.ddj.dao.BalanceSnapshotRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjBalanceSnapshot;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BalanceSnapshotImpl extends BaseServiceImpl<BalanceSnapshot, DdjBalanceSnapshot, Long> implements IBalanceSnapshotService {

    @Autowired
    private BalanceSnapshotRepository dao;
    @Autowired
    private UserRepository userDao;
    @Autowired
    private PageUtils pageUtils;

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjBalanceSnapshot model2Entity(BalanceSnapshot model) {
        DdjBalanceSnapshot ddjBalanceSnapshot = null;
        if (isNotEmpty(model)) {
            ddjBalanceSnapshot = new DdjBalanceSnapshot();
            ddjBalanceSnapshot.setBalance(model.getBalance());
            ddjBalanceSnapshot.setLockMoney(model.getLockMoney());
            ddjBalanceSnapshot.setUserId(model.getUserId());
            ddjBalanceSnapshot.setUsername(model.getUsername());
            ddjBalanceSnapshot.setId(model.getId());
        }
        return ddjBalanceSnapshot;
    }

    @Override
    protected BalanceSnapshot entity2model(DdjBalanceSnapshot entity) {
        BalanceSnapshot balanceSnapshot = null;
        if (isNotEmpty(entity)) {
            balanceSnapshot = new BalanceSnapshot();
            balanceSnapshot.setBalance(entity.getBalance());
            balanceSnapshot.setCreateTime(entity.getCreateTime());
            balanceSnapshot.setLockMoney(entity.getLockMoney());
            balanceSnapshot.setUserId(entity.getUserId());
            balanceSnapshot.setUsername(entity.getUsername());
            balanceSnapshot.setId(entity.getId());
        }
        return balanceSnapshot;
    }

    @Override
    @Transactional
    public void balanceSnapshot() {
        List<DdjUser> all = userDao.findAll();
        List<DdjBalanceSnapshot> saves = new ArrayList<>();
        for (DdjUser ddjUser : all) {
            DdjBalanceSnapshot ddjBalanceSnapshot = new DdjBalanceSnapshot();
            ddjBalanceSnapshot.setUsername(ddjUser.getUsername());
            ddjBalanceSnapshot.setUserId(ddjUser.getId());
            ddjBalanceSnapshot.setLockMoney(ddjUser.getLockMoney());
            ddjBalanceSnapshot.setBalance(ddjUser.getBalance());
            saves.add(ddjBalanceSnapshot);
        }
        dao.save(saves);
    }

    @Override
    public PageResp<BalanceSnapshot> query(BalanceSnapshot balanceSnapshot, PageReq pageReq) {
        PageRequest pageRequest = pageUtils.pageReq2PageRequest(pageReq);
        Page<DdjBalanceSnapshot> page = dao.findAll(new Specification<DdjBalanceSnapshot>() {
            @Override
            public Predicate toPredicate(Root<DdjBalanceSnapshot> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (isNotEmpty(balanceSnapshot.getCreateTime())) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(balanceSnapshot.getCreateTime());
                    System.out.println(calendar.getTime());
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    System.out.println(calendar.getTime());
                    Date beginTime = calendar.getTime();
                    calendar.add(Calendar.HOUR_OF_DAY, 1);
                    Date endTime = calendar.getTime();
                    predicates.add(criteriaBuilder.between(root.get("createTime"), beginTime, endTime));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, pageRequest);
        return page2pageResp(page);
    }
}
